package com.cskaoyan.controller.vx;


import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.po.wxPo.MarketUser;
import com.cskaoyan.bean.vo.vxVo.UserInfo;
import com.cskaoyan.bean.vo.vxVo.WxLoginUserData;
import com.cskaoyan.config.shiro.MallToken;
import com.cskaoyan.util.EncryptUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("wx/auth")
public class WxAuthController {

    @PostMapping("login")
    public BaseRespVo login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");

        Subject subject = SecurityUtils.getSubject();
        subject.login(new MallToken(username, EncryptUtils.encrypt(password),"wx"));
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        System.out.println("marketUser = " + marketUser);
        WxLoginUserData wxLoginUserData = new WxLoginUserData();
        UserInfo userInfo = new UserInfo();
        userInfo.setAvatarUrl(marketUser.getAvatar());
        userInfo.setNickName(marketUser.getNickname());
        wxLoginUserData.setUserInfo(userInfo);
        wxLoginUserData.setToken((String) subject.getSession().getId());
        return BaseRespVo.ok(wxLoginUserData);
    }

    @RequestMapping("logout")
    public BaseRespVo logout(@RequestBody Map map, HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        System.out.println("marketUser.getPassword() = " + marketUser.getPassword());
        subject.logout();
        return BaseRespVo.ok(null);
    }
}
